package com.example.administrator.nl.fragment.shopping;


import android.nfc.Tag;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.nl.R;
import com.example.administrator.nl.adapter.CommodityAdapter;
import com.example.administrator.nl.bean.CommodityBean;
import com.example.administrator.nl.bean.CommodityDetailsBean;
import com.example.administrator.nl.util.GlideImageLoader;
import com.example.administrator.nl.util.LogUtil;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class CommodityFragment extends Fragment {
    private String TAG = "CommodityFragment";
    private Unbinder unbinder;

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    View headView;


    Banner banner;


    List<Integer> listImages;
    List<CommodityBean> commodityBeanArrayList;

    private CommodityAdapter commodityAdapter;

    String bind;

    public CommodityFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        bind =  getArguments().getString("type");
        LogUtil.Log_D(TAG, "onCreateView"+bind);
        View view = inflater.inflate(R.layout.fragment_commodity, container, false);
        unbinder = ButterKnife.bind(this, view);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        recyclerView.setLayoutManager(gridLayoutManager);
        headView = LayoutInflater.from(getActivity()).inflate(R.layout.layout_banner, null);
        banner = headView.findViewById(R.id.banner_layout);
        commodityBeanArrayList = new ArrayList<>();
        commodityBeanArrayList = getData();
        commodityAdapter = new CommodityAdapter(R.layout.item_home_layout, R.layout.item_head_shopping, commodityBeanArrayList);

        commodityAdapter.addHeaderView(headView);
        recyclerView.setAdapter(commodityAdapter);


        banner.setImageLoader(new GlideImageLoader());

        listImages = new ArrayList<>();
        listImages.add(R.mipmap.ic_banner);
        listImages.add(R.mipmap.ic_banner);
        listImages.add(R.mipmap.ic_banner);
        banner.setImages(listImages);
        banner.setBannerAnimation(Transformer.CubeIn);
        banner.setBannerStyle(BannerConfig.NOT_INDICATOR);

        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(3000);
        //banner设置方法全部调用完毕时最后调用
        banner.start();

        return view;
    }


    @Override
    public void onStart() {
        super.onStart();
        LogUtil.Log_D(TAG, "onStart"+bind);

        if (null != banner) {
            banner.startAutoPlay();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        LogUtil.Log_D(TAG, "onPause"+bind);
        if (null != banner) {
            banner.stopAutoPlay();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.Log_D(TAG, "onDestroy"+bind);
        if (null != unbinder) {
            unbinder.unbind();
            ;
        }
    }

    public List<CommodityBean> getData() {
        for (int i = 0; i < 20; i++) {
            commodityBeanArrayList.add(new CommodityBean(true, "标题" + i, true, "标题" + i, "这是尾" + i));
            commodityBeanArrayList.add(new CommodityBean(new CommodityDetailsBean("url" + i, "title" + i, "money" + i, "number" + i)));
            commodityBeanArrayList.add(new CommodityBean(new CommodityDetailsBean("url" + i, "title" + i, "money" + i, "number" + i)));
            commodityBeanArrayList.add(new CommodityBean(new CommodityDetailsBean("url" + i, "title" + i, "money" + i, "number" + i)));
            commodityBeanArrayList.add(new CommodityBean(new CommodityDetailsBean("url" + i, "title" + i, "money" + i, "number" + i)));
        }
        return commodityBeanArrayList;
    }
}
